Agenda
In this post I would like to introduce ggmap which is a sister package to ggplot. To give a little bit of background, ggmap package was designed by David Kahle and Hadley Wickham with the goal of creating an easier way to visualize spacial data and models by overlaying the data on top of static maps from various online sources such as Google Maps. I will be looking into a few different functions provided by ggmap package and explore the capabilities of R in visualizing statistical data with spacial aspect.
To do this, I would like to introduce here a curious data that can be found on this website. You might need to register with Facebook. Data is in the third tab - salaries-by-region. The dataset, among other variables, contains records of starting salaries for college graduates by college. I will set a goal of visualizing the levels of salary around the US in different ways. However, the post would primarily focus on exploring ggmap capabilities for easy mapping.
Let’s install the necessary packages as marked below:
#install.packages("ggmap")
library(ggmap)
library(dplyr)
library(ggplot2)
library(readr)
Now, as a warmup lets try qmap() function. It allows the user to enter the single argument (location = ‘desired loaction’) to retrieve a map of the indicated location. For instance, below is the map of Berkeley, called by a simple code.
qmap(location = "Berkeley")
Slightly complicating our examle, we add an argument zoom = numeric to zoom in onto a segment of the map. It is best to play around with the numeric value for zoom to get a feel for how big or small a value you need for a particular purpose. Below, I zoomed in to identify the location of UC Berkeley.
qmap(location = "Berkeley", zoom = 14)
Moreover, the qmap() function allows us to specify aesthetic parameter. For instance, it is possible to (but why would you ever?) make the map appear in watercolor by simply telling qmap the maptype.
qmap(location = "Berkeley", zoom = 14, maptype = "watercolor")
More practical example of this argument specification is mapetype=“hybrid” that returns a satalite view map.
qmap(location = "Berkeley", zoom = 14, maptype = "hybrid")